function [negloglik] = val_3_44(hyps, func, n, n_class, X, y, approxF)
% a function evaluating equation 3.44 and derivatives, of R&W
% by Mark Norrish, 2011
% note: actually uses a bunch of other equations, esp. for derivations

dim = length(hyps) / n_class;
if n_class == 0
  disp('n_class == 0');
end
Hyps = reshape(hyps, dim, n_class);

bigK = zeros(n*n_class); K = zeros(n,n,n_class); sigma_noise = 1e-7;
for c = 1:n_class
  K(:,:,c) = func(Hyps(:,c), X, X) + sigma_noise*eye(n);
  bigK(1+(c-1)*n:c*n,1+(c-1)*n:c*n) = K(:,:,c);
end 

if nargin <= 6
  f = alg_3_3(n, n_class, K, y);
else
  f = alg_3_3(n, n_class, K, y, approxF);
end
F = reshape(f, n, n_class);
expsum = repmat(sum(exp(F)')',n_class,1);

pi = exp(f)./expsum;
bigPi = zeros(n_class*n,n);
for i = 1:n_class
  bigPi((i-1)*n+1:i*n,:) = diag(pi((i-1)*n+1:i*n));
end
W = diag(pi) - bigPi * bigPi';

negloglik = 0;%sum(log(sum(exp(F')))) - y' * f; 
fKf = 0;
for c = 1:n_class
  sqrtWc = chol(W(1+(c-1)*n:c*n,1+(c-1)*n:c*n) + sigma_noise*eye(n));
  negloglik = negloglik + sum(log(diag(chol(eye(n) + sqrtWc' * K(:,:,c) * sqrtWc))));
  %fKf = fKf + f(1+(c-1)*n:c*n)' / K(:,:,c) * f(1+(c-1)*n:c*n);
end 

%negloglik = 0.5 * fKf + negloglik;